package com.demostic.demostic_test.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.accloud.cloudservice.AC;
import com.accloud.cloudservice.PayloadCallback;
import com.accloud.service.ACException;
import com.accloud.service.ACUserInfo;
import com.demostic.demostic_test.R;
import com.demostic.demostic_test.base.BaseActivity;
import com.demostic.demostic_test.utils.MyLog;
import com.demostic.demostic_test.utils.ToastUtils;
import com.demostic.demostic_test.utils.UserUtils;

public class LoginActivity extends BaseActivity implements View.OnClickListener {
    public static final String IS_RESETPW = "IS_RESETPW";
    public static final String MODE = "mode_reset";
    private final String TAG = LoginActivity.class.getSimpleName();
    private Button bt_login;
    private Context context;
    private EditText et_account;
    private EditText et_password;
    private ImageView image_back;
    private ImageView image_show_pass;
    private String str_account;
    private String str_passWord;
    private TextView tv_forget;
    private TextView tv_register;

    @Override // com.demostic.demostic_test.base.BaseActivity, android.support.v7.app.AppCompatActivity, android.support.v4.app.FragmentActivity, android.app.Activity
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initView();
    }

    private void initView() {
        this.context = this;
        this.image_back = (ImageView) findViewById(R.id.image_back);
        this.image_show_pass = (ImageView) findViewById(R.id.image_show_pass);
        this.et_account = (EditText) findViewById(R.id.tv_ssid);
        this.et_password = (EditText) findViewById(R.id.et_password);
        this.et_password.setTransformationMethod(PasswordTransformationMethod.getInstance());
        this.tv_forget = (TextView) findViewById(R.id.tv_resetPw);
        this.tv_register = (TextView) findViewById(R.id.tv_register);
        this.bt_login = (Button) findViewById(R.id.bt_login);
        this.et_password.addTextChangedListener(new MyTextWatcher());
        this.image_back.setOnClickListener(this);
        this.image_show_pass.setOnClickListener(this);
        this.tv_forget.setOnClickListener(this);
        this.tv_register.setOnClickListener(this);
        this.bt_login.setOnClickListener(this);
    }

    @Override // android.view.View.OnClickListener
    public void onClick(View v) {
        boolean isSelected = true;
        switch (v.getId()) {
            case R.id.image_back /* 2131755189 */:
                finish();
                return;
            case R.id.image_show_pass /* 2131755202 */:
                int currentIndex = this.et_password.getSelectionStart();
                if (this.image_show_pass.isSelected()) {
                    isSelected = false;
                }
                this.image_show_pass.setSelected(isSelected);
                if (isSelected) {
                    this.et_password.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                } else {
                    this.et_password.setTransformationMethod(PasswordTransformationMethod.getInstance());
                }
                this.et_password.setSelection(currentIndex);
                return;
            case R.id.bt_login /* 2131755277 */:
                this.str_account = this.et_account.getText().toString().trim();
                this.str_passWord = this.et_password.getText().toString().trim();
                if (TextUtils.isEmpty(this.str_account)) {
                    ToastUtils.showToast(this.context, getString(R.string.login_aty_input_email));
                    return;
                } else if (!UserUtils.isEmail(this.str_account)) {
                    ToastUtils.showToast(this.context, getString(R.string.register1_aty_email_wrong));
                    return;
                } else if (TextUtils.isEmpty(this.str_passWord)) {
                    ToastUtils.showToast(this.context, getString(R.string.login_aty_input_pw));
                    return;
                } else if (this.str_passWord.length() < 6 || this.str_passWord.length() > 18) {
                    ToastUtils.showToast(this.context, getString(R.string.reg_aty_error_char));
                    return;
                } else {
                    checkExist(this.str_account);
                    return;
                }
            case R.id.tv_resetPw /* 2131755278 */:
//                Intent intent_res = new Intent(this, RegisterActivity1.class);
//                intent_res.putExtra(IS_RESETPW, true);
//                startActivity(intent_res);
                return;
            case R.id.tv_register /* 2131755279 */:
//                Intent intent = new Intent(this, RegisterActivity1.class);
//                intent.putExtra(IS_RESETPW, false);
//                startActivity(intent);
                return;
            default:
                return;
        }
    }

    private void checkExist(String account) {
        AC.accountMgr().checkExist(account, new PayloadCallback<Boolean>() {
            /* class com.demostic.demostic_test.activity.LoginActivity.AnonymousClass1 */

            public void success(Boolean isExist) {
                if (isExist.booleanValue()) {
                    LoginActivity.this.toLogin(LoginActivity.this.str_account, LoginActivity.this.str_passWord);
                } else {
                    ToastUtils.showToast(LoginActivity.this.context, LoginActivity.this.getString(R.string.register1_aty_unknow_address));
                }
            }

            @Override // com.accloud.cloudservice.BaseCallback
            public void error(ACException e) {
                ToastUtils.showErrorToast(LoginActivity.this.context, e.getErrorCode());
            }
        });
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void toLogin(String account, String password) {
        AC.accountMgr().login(account, password, new PayloadCallback<ACUserInfo>() {
            /* class com.demostic.demostic_test.activity.LoginActivity.AnonymousClass2 */

            public void success(ACUserInfo userInfo) {
                ToastUtils.showToast(LoginActivity.this.context, LoginActivity.this.getString(R.string.login_success));
                LoginActivity.this.finish();
            }

            @Override // com.accloud.cloudservice.BaseCallback
            public void error(ACException e) {
                MyLog.e(LoginActivity.this.TAG, "error:" + e.toString());
                if (e.getErrorCode() == 3504) {
                    ToastUtils.showToast(LoginActivity.this.context, LoginActivity.this.getString(R.string.login_pasw_wrong));
                }
            }
        });
    }

    public class MyTextWatcher implements TextWatcher {
        private final int charMaxNum = 18;
        private int editEnd;
        private int editStart;
        private CharSequence temp;

        public MyTextWatcher() {
        }

        @Override // android.text.TextWatcher
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            this.temp = s;
        }

        @Override // android.text.TextWatcher
        public void onTextChanged(CharSequence s, int start, int before, int count) {
        }

        @Override // android.text.TextWatcher
        public void afterTextChanged(Editable s) {
            this.editStart = LoginActivity.this.et_password.getSelectionStart();
            this.editEnd = LoginActivity.this.et_password.getSelectionEnd();
            if (this.temp.length() > 18) {
                ToastUtils.showToast(LoginActivity.this.context, LoginActivity.this.getString(R.string.login_pasw_limit));
                s.delete(this.editStart - 1, this.editEnd);
                int tempSelection = this.editStart;
                LoginActivity.this.et_password.setText(s);
                LoginActivity.this.et_password.setSelection(tempSelection);
            }
        }
    }
}
